<?php

define("ADMINISTRATOR", 2);
define("USER", 3);
define("URL", "http://".$_SERVER["HTTP_HOST"]."/galactus/gtm/");

require_once("classes/Model/GalactusModel.class.php");
require_once("classes/Model/GalactusModelTelescope.class.php");
require_once("classes/Model/GalactusModelCalendar.class.php");
require_once("classes/Model/GalactusModelPost.class.php");
require_once("classes/Model/GalactusModelGallery.class.php");
require_once("classes/Model/GalactusModelControl.class.php");
require_once("classes/Model/GalactusModelUser.class.php");
require_once("classes/Model/GalactusModelAccount.class.php");
require_once("classes/View/GalactusViewHelper.class.php");
require_once("classes/View/GalactusView.class.php");
require_once("classes/View/GalactusViewAccount.class.php");
require_once("classes/View/GalactusViewCalendar.class.php");
require_once("classes/View/GalactusViewControl.class.php");
require_once("classes/View/GalactusViewPost.class.php");
require_once("classes/View/GalactusViewGallery.class.php");
require_once("classes/View/GalactusViewPresentation.class.php");
require_once("classes/View/GalactusViewSchedule.class.php");
require_once("classes/View/GalactusViewTeam.class.php");
require_once("classes/View/GalactusViewTelescope.class.php");
require_once("classes/View/GalactusViewCalendar.class.php");
require_once("classes/View/GalactusViewUser.class.php");

class GalactusControl {

	public $model = NULL;
	public $view = NULL;
	public $user_id = NULL;
	public $profile_id = NULL;

	/*
	** CONSTRUCTOR
	*/

	public function __construct( ) {
		session_start( );
		if(isset($_SESSION['user_id']) && isset($_SESSION['profile_id'])) {
			$this->model = new GalactusModelUser($this);
			$sql = "SELECT id, profile_id FROM User WHERE id = :uid AND profile_id = :pid AND session_id = :sid AND active = 1";
			$params = array(":uid" => $_SESSION["user_id"], ":pid" => $_SESSION["profile_id"], ":sid" => session_id( ));
			$query = $this->model->query($sql, $params);
			if(!$query["fail"] && !empty($query["result"])) {
				$this->user_id = $_SESSION["user_id"];
				$this->profile_id = $_SESSION["profile_id"];
			}
		}
	}

	public function execute($command, $get = NULL, $post = NULL, $files = NULL) {
		try {
			$this->model	= new GalactusModel($this);
			$this->view	= new GalactusView($this);
			switch($this->profile_id.$command[0]) {
				case ADMINISTRATOR."ACCOUNT": $this->executeAccount($command, $post); break;
				case ADMINISTRATOR."CALENDAR": $this->executeCalendar($command, $post); break;
				case ADMINISTRATOR."CONTROL":
				case USER."CONTROL": $this->executeControl($command, $post); break;
				case $this->profile_id."GALLERY": $this->executeGallery($command, $post); break;				
				case $this->profile_id."PRESENTATION": $this->executePresentation( ); break;
				case ADMINISTRATOR."POST": $this->executePost($command, $post); break;
				case $this->profile_id."REDIRECT": $this->view->show($command[1]); break;
				case $this->profile_id."RETRIEVE_CSS": $this->view->retrieveCSS( ); break;
				case $this->profile_id."RETRIEVE_JS": $this->view->retrieveJS( ); break;
				case ADMINISTRATOR."SCHEDULE":
				case USER."SCHEDULE": $this->executeSchedule($command, $post); break;
				case $this->profile_id."TEAM": $this->executeTeam( ); break;
				case $this->profile_id."TELESCOPE": $this->executeTelescope($command, $get, $post, $files); break;
				case $this->profile_id."USER": $this->executeUser($command, $post); break;
				default: $this->view->show( ); break;
			}
		}
		catch(Exception $e) {
			$this->view->helper->alertError($e->message);
		}
	}

	private function executeAccount($command, $post) {
		$this->model = new GalactusModelAccount($this);
		$this->view	= new GalactusViewAccount($this);
		switch($command[1]) {
			case "ADD":  $this->model->add($post[0], $post[1], $post[2], $post[3], $post[4], $post[5]); break;
			case "CHANGE_PASSWORD": $this->model->changePassword($post); break;
			case "CHANGE_PROFILE": $this->model->changeProfile($post); break;
			case "REMOVE": $this->model->remove($post); break;
			default: $this->view->show($command, $post); break;
		}
	}	

	private function executeCalendar($command, $post) {
		$this->model = new GalactusModelCalendar($this);
		$this->view	= new GalactusViewCalendar($this);
		switch($command[1]) {
			case "ADD_EVENTS": $this->model->addEvents($post); break;
			default: $this->view->show($post); break;
		}
	}

	private function executeControl($command, $post) {
		$this->model = new GalactusModelControl($this);
		$this->view	= new GalactusViewControl($this);
		switch($command[1]) {
			case "GET_AZ_DEC": $this->model->getAzAlt($post[0], $post[1]); break;
			case "GET_RA_DEC": $this->model->getRaDec($post[0], $post[1]); break;
			case "GOTO_AZ_ALT": $this->model->gotoAzAlt($post[0], $post[1], $post[2]); break;
			case "GOTO_RA_DEC": $this->model->gotoRaDec($post[0], $post[1], $post[2]); break;
			case "SNAPSHOT": $this->model->snapshot($post); break;
			default: $this->view->show($post); break;
		}
	}

	private function executeGallery($command, $post) {
		$this->model = new GalactusModelGallery($this);
		$this->view	= new GalactusViewGallery($this);
		switch($this->profile_id.$command[1]) {
			case ADMINISTRATOR."ADD_CONTENT":
			case USER."ADD_CONTENT": $this->model->addContent($post[0], $post[1], $post[2], $post[3]); break;
			case ADMINISTRATOR."REMOVE_CONTENT":
			case USER."REMOVE_CONTENT": $this->model->removeContent($post); break;
			case $this->profile_id."RETRIEVE_CONTENT": $this->view->retrieveContent($get); break;
			default: $this->view->show( ); break;
		}
	}

	private function executePresentation( ) {
		$this->view	= new GalactusViewPresentation($this);
		$this->view->show( );
	}

	private function executePost($command = NULL, $post = NULL) {
		$this->model = new GalactusModelPost($this);
		$this->view	= new GalactusViewPost($this);
		switch($command[1]) {
			case "ADD": $this->model->add($post[0], $post[1]);
			case "REMOVE": $this->model->remove($post);
			default: $this->view->show( ); break;
		}
	}

	private function executeSchedule($command, $post) {
		$this->model = new GalactusModelCalendar($this);
		$this->view	= new GalactusViewSchedule($this);
		switch($command[1]) {
			case "SCHEDULE": $this->model->schedule($post); break;
			default: $this->view->show($post); break;
		}
	}

	private function executeTeam( ) {
		$this->view	= new GalactusViewTeam($this);
		$this->view->show( );
	}

	private function executeTelescope($command, $get = NULL, $post = NULL, $files = NULL) {
		$this->model = new GalactusModelTelescope($this);
		$this->view	= new GalactusViewTelescope($this);
		switch($this->profile_id.$command[1]) {
			case ADMINISTRATOR."ADD": $this->model->add($post[1], $post[2], $post[3], $post[4], $post[5], $files["tmp_name"][0], $files["type"][0]); break;
			case ADMINISTRATOR."EDIT": $this->model->edit($post[0], $post[1], $post[2], $post[3], $post[4], $post[5], $files["tmp_name"][0], $files["type"][0]); break;
			case ADMINISTRATOR."REMOVE": $this->model->remove($post); break;
			case ADMINISTRATOR."STREAM_START": $this->model->start( ); $this->view->helper->alertSuccess("Telescópios ativados!", URL.'REDIRECT/TELESCOPE/SHOW'); break;
			case ADMINISTRATOR."STREAM_STOP": $this->model->stop( ); $this->view->helper->alertSuccess("Telescópios desativados!", URL.'REDIRECT/TELESCOPE/SHOW'); break;
			case $this->profile_id."RETRIEVE_IMAGE": $this->view->retrieveImage($get); break;
			default: $this->view->show($command, $post); break;
		}
	}

	private function executeUser($command, $post = NULL) {
		$this->model = new GalactusModelUser($this);
		$this->view	= new GalactusViewUser($this);
		switch($this->profile_id.$command[1]) {
			case $this->profile_id."ACTIVE": $this->model->active($get[0], $get[1]); break;
			case ADMINISTRATOR."CHANGE_PASSWORD":
			case USER."CHANGE_PASSWORD": $this->model->changePassword($post[0], $post[1]); break;
			case ADMINISTRATOR."EDIT":
			case USER."EDIT": $this->model->edit($post[0], $post[1]); break;
			case ADMINISTRATOR."REMOVE":
			case USER."REMOVE": $this->model->remove( ); break;
			case $this->profile_id."SIGN_IN": $this->model->signIn($post[0], $post[1]); break;
			case $this->profile_id."SIGN_OUT": $this->model->signOut( ); break;
			case $this->profile_id."SIGN_UP": $this->model->signUp($post[0], $post[1], $post[2]); break;
			default: $this->view->show($command); break;
		}		
	}

}

?>
